#
# smartglade 	v0.9.0 	14:30 	03 Nov 2007	pyrgen
# smartglade 	v0.91..0 	20:59 	04 Nov 2007	pyrgen
# smartglade 	v0.92.0 	16:55 	07 Nov 2007	pyrgen
#
# by Python Pyrgen
#

try:
	import pygtk
	pygtk.require ("2.0")
	import gtk
	import gtk.gdk
	import gtk.glade
	import gobject
except:
	sys.exit (1)

try:
	import xml.dom.ext
	import xml.dom.minidom
	from xml.dom.minidom import parse, parseString
except:
	sys.exit (1)

# utils
def widget_correct (widget_id):
	widget_correct_id = widget_id.lower()
	widget_correct_id = widget_correct_id.replace(' ', '_')
	widget_correct_id = widget_correct_id.replace('!', '_')
	widget_correct_id = widget_correct_id.replace('@', '_')
	widget_correct_id = widget_correct_id.replace('#', '_')
	widget_correct_id = widget_correct_id.replace('$', '_')
	widget_correct_id = widget_correct_id.replace('%', '_')
	widget_correct_id = widget_correct_id.replace('^', '_')
	widget_correct_id = widget_correct_id.replace('*', '_')
	widget_correct_id = widget_correct_id.replace('(', '_')
	widget_correct_id = widget_correct_id.replace(')', '_')
	widget_correct_id = widget_correct_id.replace('-', '_')
	widget_correct_id = widget_correct_id.replace('+', '_')
	widget_correct_id = widget_correct_id.replace('=', '_')
	widget_correct_id = widget_correct_id.replace('\\', '_')
	widget_correct_id = widget_correct_id.replace('|', '_')
	widget_correct_id = widget_correct_id.replace('[', '_')
	widget_correct_id = widget_correct_id.replace(']', '_')
	widget_correct_id = widget_correct_id.replace('{', '_')
	widget_correct_id = widget_correct_id.replace('}', '_')
	widget_correct_id = widget_correct_id.replace(';', '_')
	widget_correct_id = widget_correct_id.replace(':', '_')
	widget_correct_id = widget_correct_id.replace('"', '_')
	widget_correct_id = widget_correct_id.replace(',', '_')
	widget_correct_id = widget_correct_id.replace('.', '_')
	widget_correct_id = widget_correct_id.replace('<', '_')
	widget_correct_id = widget_correct_id.replace('>', '_')
	widget_correct_id = widget_correct_id.replace('/', '_')
	widget_correct_id = widget_correct_id.replace('?', '_')
	widget_correct_id = widget_correct_id.replace('`', '_')
	widget_correct_id = widget_correct_id.replace('~', '_')
	return widget_correct_id

# XML
class __xml__:
	# default type is FILE, and it means that uses external GLADE file
	type = 'file'
	root = None
	domain = None
	found_widget = False
	
	def __init__ (self, type, data):
		if type == 'file':
			self.filename_glade = filename_glade = data
			f = open(filename_glade)
			self.glade_buffer = glade_buffer = f.read()
			f.close()
		
		elif type == 'buffer':
			self.filename_glade = None
			self.glade_buffer = glade_buffer = data
		
		self.type = type
	
	# auto-get widget(s) by widget_id (== root)
	def get(self, root=None, domain=None):
		self.root = root
		self.domain = domain
		type = self.type
		
		if type == 'file':
			self.xml = xml = gtk.glade.XML(self.filename_glade, root, domain)
			doc = parse(self.filename_glade)
		elif type == 'buffer':
			self.xml = xml = gtk.glade.xml_new_from_buffer(self.glade_buffer, len(self.glade_buffer), root, domain)
			doc = parseString(self.glade_buffer)
		
		for n1 in doc.childNodes:
			if n1.nodeType == n1.ELEMENT_NODE:
				if n1.tagName == 'glade-interface':
					self.find_widget(n1)
					break
		
		#~ print '------------------------'
	
	def find_widget(self, parent):
		for n1 in parent.childNodes:
			if n1.nodeType == n1.ELEMENT_NODE:
				if n1.tagName == 'widget':
					widget_id = n1.getAttribute('id')
					widget_class = n1.getAttribute('class')
					
					if widget_id == self.root:
						expr = ('self.%s = self.xml.get_widget("%s")') % (widget_correct(widget_id), widget_id)
						#~ print expr
						exec (expr)
						
						self.get_next(n1)
						break
				
				elif n1.tagName == 'property':
					pass
					
				elif n1.tagName == 'child':
					if n1.childNodes:
						self.get_next(n1)
	
	def get_next(self, parent):
		for n1 in parent.childNodes:
			if n1.nodeType == n1.ELEMENT_NODE:
				if n1.tagName == 'widget':
					widget_id = n1.getAttribute('id')
					widget_class = n1.getAttribute('class')
					
					expr = ('self.%s = self.xml.get_widget("%s")') % (widget_correct(widget_id), widget_id)
					#~ print expr
					exec (expr)
					
					self.get_next(n1)
				
				elif n1.tagName == 'property':
					pass
					
				elif n1.tagName == 'child':
					if n1.childNodes:
						self.get_next(n1)
	
class xml_new_from_buffer (__xml__):
	def __init__ (self, glade_buffer):
		__xml__.__init__ (self, 'buffer', glade_buffer)
		
class xml (__xml__):
	def __init__ (self, filename_glade):
		__xml__.__init__ (self, 'file', filename_glade)
